package com.jic.point.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.model.entity.ConfPlatform;
import com.jic.point.model.entity.ConfPlatformApply;
import com.jic.point.model.vo.request.ConfPlatformApplyRequest;
import com.jic.point.model.vo.response.ConfPlatformApplyResponse;
import com.jic.point.service.ConfPlatformApplyService;
import com.jic.point.service.ConfPlatformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/confPlatformApply")
@Api(tags = " [子平台表]控制类-PC")
public class ConfPlatformApplyController {

    @Autowired
    private ConfPlatformApplyService service;

    @Autowired
    private ConfPlatformService confPlatformService;

    /**
     *新增记录
     * @param request 新增[子平台表]请求参数
     * @return ConfPlatformResponse 新增[子平台表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[子平台表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody ConfPlatformApplyRequest request) throws Throwable {
        ConfPlatformApply confPlatformApply = new ConfPlatformApply();
        BeanUtils.copyProperties(request, confPlatformApply);
        long flag = service.save(confPlatformApply);
        return RestResult.success(flag);
    }




    /**
     * 新增或修改记录
     * @param request 新增或修改[子平台表]请求参数
     * @return ConfPlatformResponse 新增或修改[子平台表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[子平台表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody ConfPlatformApplyRequest request) throws Throwable {
        ConfPlatformApply confPlatformApply = new ConfPlatformApply();
        BeanUtils.copyProperties(request, confPlatformApply);
        long flag =   service.saveOrUpdate(confPlatformApply);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     * @param request 删除[子平台表]请求参数
     * @return ConfPlatformResponse 删除[子平台表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[子平台表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody ConfPlatformApplyRequest request) throws Throwable {
        ConfPlatformApply confPlatformApply = new ConfPlatformApply();
        BeanUtils.copyProperties(request, confPlatformApply);
        long flag = service.deleteByPrimaryKeyLogically(confPlatformApply);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     * @param request 修改[子平台表]请求参数
     * @return ConfPlatformResponse 修改[子平台表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[子平台表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody ConfPlatformApplyRequest request) throws Throwable {
        ConfPlatformApply confPlatformApply = new ConfPlatformApply();
        BeanUtils.copyProperties(request, confPlatformApply);
        long flag = service.updateByPrimaryKeySelective(confPlatformApply);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     * @param request 查询[子平台表]详情请求参数
     * @return ConfPlatformResponse 查询[子平台表]详情响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[子平台表]详情")
    @ResponseBody
    public RestResult<ConfPlatformApplyResponse> getByPrimaryKey(@Validated  @RequestBody ConfPlatformApplyRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request!=null && request.getId()!=null) {

            ConfPlatformApply confPlatformApply = new ConfPlatformApply();
            BeanUtils.copyProperties(request, confPlatformApply);
            ConfPlatformApply result = service.getByPrimaryKey(confPlatformApply);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            ConfPlatformApplyResponse bo = new ConfPlatformApplyResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new ConfPlatformApplyResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageInfo<ConfPlatformApply>> listPage (@RequestBody ConfPlatformApplyRequest request) throws Throwable {
        ConfPlatformApply param = new ConfPlatformApply();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        PageInfo<ConfPlatformApply> confPlatformApplyPageInfo = service.listPageBySelective(param, request.getPageNum(), request.getPageSize());
        return RestResult.success(confPlatformApplyPageInfo);
    }


    /**
     *CONF_PLATFORM 查询列表（不分页）
     * @param  request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [子平台表] ")
    @ResponseBody
    public RestResult<List<ConfPlatformApplyResponse>>  listConfPlatformAllRecord(@RequestBody ConfPlatformApplyRequest request) throws Throwable {
        ConfPlatformApply param = new ConfPlatformApply();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<ConfPlatformApply> list = service.listAllRecord(param);
        List<ConfPlatformApplyResponse> responseList = list.stream().map(temp -> {
            ConfPlatformApplyResponse bo = new ConfPlatformApplyResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     * 审核提交
     */
    @PostMapping("/auditAubmit")
    @ResponseBody
    public RestResult<Long> auditAubmit(@Validated @RequestBody ConfPlatformApplyRequest request ) throws Throwable {
        log.info("查询冻结与解冻参数入参，{}", JSONObject.toJSONString(request));
        ConfPlatformApply confPlatformApply = new ConfPlatformApply();
        BeanUtils.copyProperties(request, confPlatformApply);
        ConfPlatformApply result = service.getByPrimaryKey(confPlatformApply);
        //赋值
        result.setAuditSts(request.getAuditSts());
        result.setAuditReason(request.getAuditReason());
        result.setApplierId(request.getAdminOperate().getOperator());
        if (result.getAuditSts() == 1) {
            if (result.getCurrentAction() == 0) {
                //申请表逻辑删除
                result.setDeleteFlag(1);
                service.deleteByPrimaryKeyLogically(result);
                //插入到主表中
                ConfPlatform confPlatform = new ConfPlatform();
                BeanUtils.copyProperties(result, confPlatform);
                confPlatform.setId(null);
                confPlatform.setDeleteFlag(0);
                long flag = confPlatformService.insertSelective(confPlatform);
                return RestResult.success(flag);
            } else if (result.getCurrentAction() == 1) {
                //修改成功
                //申请表逻辑删除
                result.setDeleteFlag(1);
                service.deleteByPrimaryKeyLogically(result);
                //主表----通过code修改delete_flag为0
                ConfPlatform confPlatform = new ConfPlatform();
                BeanUtils.copyProperties(result, confPlatform);
                confPlatform.setDeleteFlag(0);
                long flag = confPlatformService.saveOrUpdateAudit(confPlatform);
                return RestResult.success(flag);
            } else if (result.getCurrentAction() == 3) {
                ConfPlatform confPlatform = new ConfPlatform();
                confPlatform.setId(request.getId());
                confPlatform.setSts(3);
                service.updateByPrimaryKeySelective(result);
            } else {
                result.setDeleteFlag(1);
                long flag = service.deleteByPrimaryKeyLogically(result);
                return RestResult.success(flag);
            }
        } else if(result.getAuditSts() == 2) {
            if (result.getCurrentAction() == 0) {
                result.setDeleteFlag(1);
                long flag = service.deleteByPrimaryKeyLogically(result);
                return RestResult.success(flag);
            } else {
                //删除、编辑失败，申请表逻辑删除，主表逻辑删除改为1
                result.setDeleteFlag(1);
                service.deleteByPrimaryKeyLogically(result);
                //主表----通过code修改delete_flag为0
                ConfPlatform confPlatform = new ConfPlatform();
                BeanUtils.copyProperties(result, confPlatform);
                confPlatform.setDeleteFlag(0);
                long flag = confPlatformService.saveOrUpdateAudit(confPlatform);
                return RestResult.success(flag);
            }
        }else{
            result.setAuditSts(3);
            long flag = service.deleteByPrimaryKeyLogically(result);
            return RestResult.success(flag);
        }
        return null;
    }
}
